// index.js
// 获取应用实例
const app = getApp()

Page({
    data: {
        records: [],
        selectedPlayer: [],
        showLayer: false,
        showChange: false,
        bombNumbers: [0, 1, 2, 3, 4, 5, 6, 7, 8],
        roles: ['地主', '农民'],
        landlord: {},
        bombNumber: 0,
        winner: '',
        isPreview: false,

        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onLoad() {
        this.setData({
            selectedPlayer: app.globalData.selectedPlayer
        })

        if (wx.getUserProfile) {
            this.setData({
                canIUseGetUserProfile: true
            })
        }
        const obj = wx.getStorageSync('landlord')
        if(obj) this.setData(obj)
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                console.log(res)
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        })
    },
    getUserInfo(e) {
        // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
        console.log(e)
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    handleChange() {
        this.setData({
            showChange: true
        })
    },
    scoring() {
        this.setData({
            landlord: {},
            bombNumber: 0,
            winner: '',
            showLayer: true
        })
    },
    closeLayer(){
        this.setData({
            showLayer: false,
            isPreview: false
        })
    },
    back() {
        if(!this.data.records.length) return

        const _this = this
        wx.showModal({
            title: '询问',
            content: '确定回滚吗？',
            success(res) {
                if (res.confirm) {
                    const selectedPlayer = JSON.parse(JSON.stringify(_this.data.selectedPlayer))
                    const records = _this.data.records.slice(1);
                    if (records.length) {
                        selectedPlayer[0].score = records[0][1]
                        selectedPlayer[1].score = records[0][2]
                        selectedPlayer[2].score = records[0][3]
                    } else {
                        selectedPlayer[0].score = 0
                        selectedPlayer[1].score = 0
                        selectedPlayer[2].score = 0
                    }

                    _this.setData({
                        records,
                        selectedPlayer
                    })
                }
            }
        })
    },
    selectBomb(e) {
        this.setData({
            bombNumber: e.target.dataset.item
        })
    },
    selectLandlord(e) {
        this.setData({
            landlord: e.target.dataset.item
        })
    },
    selectRole(e) {
        this.setData({
            winner: e.target.dataset.item
        })
    },
    submit() {
        const {
            bombNumber,
            landlord,
            winner
        } = this.data

        if (!landlord.id) {
            wx.showToast({
                title: '谁是地主？',
                icon: 'none'
            })
            return
        };

        if (!winner) {
            wx.showToast({
                title: '谁赢了？',
                icon: 'none'
            })
            return
        }
        const baseScore = app.globalData.baseScore || 2;
        const score = baseScore * Math.pow(2, bombNumber)
        const selectedPlayer = JSON.parse(JSON.stringify(this.data.selectedPlayer))
        selectedPlayer.forEach(val => {
            if (val.id === landlord.id) {
                if (winner === '农民') {
                    val.score -= score * 2
                } else {
                    val.score += score * 2
                }
            } else {
                if (winner === '农民') {
                    val.score += score
                } else {
                    val.score -= score
                }
            }
        })

        const records = this.data.records
        const record = [records.length + 1 + '/' + bombNumber + '/' + baseScore, selectedPlayer[0].score, selectedPlayer[1].score, selectedPlayer[2].score]
        const obj = {
            selectedPlayer,
            landlord,
            bombNumber,
            winner,
            showLayer: false,
            records: [record, ...records]
        }
        this.setData(obj)

        app.globalData.selectedPlayer = selectedPlayer
        wx.setStorageSync('landlord', obj)
    },
    clickRow(e) {
        const index = e.currentTarget.dataset.index
        const target = this.data.records[index]
        const bombNumber = target[0].split('/')[1]
        console.log('target',target)
        this.setData({
            bombNumber: Number(bombNumber),
            landlord: target[5],
            winner: target[4],
            showLayer: true,
            isPreview: true
        })
    }
})
